package com.sdau.zhang.controller;

import com.sdau.zhang.aes.Aes;
import com.sdau.zhang.dao.LuntanDao;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
@Controller
public class helloController {

	@Autowired
	private LuntanDao luntanDao;

	@RequestMapping("/hi")
	@ResponseBody
	public String hellospringboot() {
		return "yes, you did it!";
	}

	@RequestMapping("/show")
	public String show(Model model)
	{
		Map<String,Object> modelMap = new HashMap<String,Object>();
		model.addAttribute("modelMap",modelMap);
		return "/Mr_z/show.html";
	}

	@ResponseBody
	@RequestMapping("/hello")
	public Map<String,Object> hello(Model model)
	{
		Map<String,Object> modelMap = new HashMap<String,Object>();
		modelMap.put("luntan",luntanDao.getTopic(1));
		return modelMap;
	}

	@RequestMapping("/shiro")
	public String  shiro() throws NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, NoSuchPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
		SecretKey secretKey= Aes.genKey();
		String s = "asdfghj";
		byte[] res = Aes.encrypt(s,secretKey);
		System.out.println(new String(res,"UTF-8"));
		String s0 = Aes.decrpt(res,secretKey);
		System.out.println("这是原始数据"+s0);
		System.out.println("-----------------底线--------------------");
		return "/Shiro_test/main.html";

	}
	@RequestMapping("shiro/search")
	public String  shiro_s()
	{
		return "/Shiro_test/search.html";
	}
	@RequestMapping("shiro/update")
	public String  shiro_u()
	{
		return "/Shiro_test/update.html";
	}
	@RequestMapping("shiro/login")
	public String shiro_login()
	{
		return "/Shiro_test/login.html";
	}
	@RequestMapping("shiro/verify")
	public String judge(String name,String passwd,Model model,HttpSession session){
		//获取subject
		Subject subject= SecurityUtils.getSubject();
		System.out.println("进入登录页面");
		 //封装用户数据
		UsernamePasswordToken token = new UsernamePasswordToken(name,passwd);
		System.out.println("进入验证方法");
		try{
			subject.login(token);
			session.setAttribute("user",token.getUsername());
			return "/Shiro_test/main.html";
		}catch(UnknownAccountException e)
		{
			model.addAttribute("msg", "用户名不存在");
			return "/Shiro_test/login.html";

		}catch (IncorrectCredentialsException e)
		{
			model.addAttribute("msg","密码错误");
			return "/Shiro_test/login.html";
		}

	}
	@RequestMapping("/shiro/noAuth")
	public String noAuth()
	{
		return "/Shiro_test/noAuth.html";
	}
	@RequestMapping("/shiro/center")
	public String center()
	{
		return "/Shiro_test/center.html";
	}

	@RequestMapping("/shiro/logout")
	public String logout(HttpSession session, Model model) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		model.addAttribute("msg", "安全退出！");
		session.removeAttribute("user");
		return "/Shiro_test/login.html";
	}

}